﻿using WebAssistant.BusinessLogic.Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Assistant.Forms
{
    

    public partial class FormHome : UITitlePage
    {
        public static FormHome homePage;

        public FormHome()
        {
            InitializeComponent();
            InitListView();
            homePage = this;
        }


        #region 日志


        public void InitListView()
        {            
            listviewLogs.GridLines = true;//显示各个记录的分隔线 
            listviewLogs.FullRowSelect = true;//要选择就是一行 
            listviewLogs.View = View.Details;//定义列表显示的方式 
            listviewLogs.Scrollable = true;//需要时候显示滚动条 
            listviewLogs.MultiSelect = false; // 不可以多行选择 
            listviewLogs.HeaderStyle = ColumnHeaderStyle.Nonclickable;
            listviewLogs.Alignment = System.Windows.Forms.ListViewAlignment.Top;
            listviewLogs.OwnerDraw = true;
            listviewLogs.Columns.Add("StartTime", "开始时间");
            listviewLogs.Columns.Add("EndTime", "结束时间");;
            listviewLogs.Columns.Add("Events", "事件");
            listviewLogs.Columns["StartTime"].Width = 120;
            listviewLogs.Columns["EndTime"].Width = 120;
            listviewLogs.Columns["Events"].Width = -2;//-1根据内容设置宽度，-2根据内容设置宽度

            //BindData();
        }

        //private void BindData()
        //{
        //    listviewLogs.Items.Clear();
        //    listviewLogs.BeginUpdate();
        //    foreach (Logs log in Config.LatestLog)
        //    {
        //        ListViewItem item = new ListViewItem();
        //        item.SubItems[0].Text = log.StartDate.ToString("yyyy-MM-dd HH:mm:ss");
        //        item.SubItems.Add(log.EndDate.ToString("yyyy-MM-dd HH:mm:ss"));
        //        item.SubItems.Add(log.MethodName + ":" + log.ServiceName);

        //        this.listviewLogs.Items.Add(item);
        //    }
        //    listviewLogs.EndUpdate(); //结束数据处理，UI界面一次性绘制
        //}

        public void AddListViewItem(Logs log)
        {
 
            ListViewItem item = new ListViewItem();
            item.SubItems[0].Text = log.StartDate.ToString("yyyy-MM-dd HH:mm:ss");
            item.SubItems.Add(log.EndDate.ToString("yyyy-MM-dd HH:mm:ss"));
            item.SubItems.Add(log.MethodName + ":" + log.ServiceName);


            //listviewLogs.Items.Add(item);
            this.listviewLogs.Items.Insert(0, item);

        }

        private void listviewLogs_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
            using (StringFormat sf = new StringFormat())
            {
                sf.Alignment = StringAlignment.Center;
                e.DrawBackground();
                e.Graphics.DrawString(e.Header.Text, listviewLogs.Font,
                        Brushes.Black, e.Bounds, sf);
            }
        }

        private void listviewLogs_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            e.DrawDefault = true;//采用系统默认方式绘制项
        }

        private void listviewLogs_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {
            e.DrawDefault = true;	//采用系统默认方式绘制子项
        }



        #endregion

        #region 窗体

        private void FormHome_SizeChanged(object sender, EventArgs e)
        {
            listviewLogs.Columns["StartTime"].Width = 130;
            listviewLogs.Columns["EndTime"].Width = 130;
            listviewLogs.Columns["Events"].Width = -2;//-1根据内容设置宽度，-2根据内容设置宽度
        }

        #endregion
    }
}
